package com.zl.freight.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zl.freight.R;
import com.zl.freight.base.BaseActivity;
import com.zl.freight.mode.MoneySettingsMode;
import com.zl.freight.utils.API;
import com.zl.freight.utils.SoapCallback;
import com.zl.freight.utils.SoapUtils;
import com.zl.zlibrary.adapter.UniversalAdapter;
import com.zl.zlibrary.adapter.UniversalViewHolder;
import com.zl.zlibrary.utils.GsonUtils;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhanglei
 */
public class MoneySettingsActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.settings_listView)
    ListView settingsListView;

    private List<MoneySettingsMode> mList = new ArrayList<>();
    private UniversalAdapter<MoneySettingsMode> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_money_settings);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        //储存配置信息如：临时发货要多少钱等
        Map<String, String> params = new HashMap<>();
        SoapUtils.Post(mActivity, API.BaseSet, params, new SoapCallback() {
            @Override
            public void onError(String error) {

            }

            @Override
            public void onSuccess(String data) {
                try {
                    JSONArray array = new JSONArray(data);
                    mList.clear();
                    for (int i = 0; i < array.length(); i++) {
                        MoneySettingsMode mode = GsonUtils.fromJson(array.optString(i), MoneySettingsMode.class);
                        if (mode.getCodeName().equals("1元兑换积分比例")) {
                            continue;
                        } else if (mode.getCodeName().equals("版本")) {
                            continue;
                        } else if (mode.getCodeName().equals("版本号")) {
                            continue;
                        } else if (mode.getCodeName().equals("平台协议")) {
                            continue;
                        } else if (mode.getCodeName().equals("阿里支付公钥")) {
                            continue;
                        } else if (mode.getCodeName().equals("阿里支付私钥")) {
                            continue;
                        }
                        mList.add(mode);
                    }

                    mAdapter.notifyDataSetChanged();
                } catch (Exception e) {

                }

            }
        });
    }

    private void initView() {
        tvTitle.setText("修改收费项目");
        mAdapter = new UniversalAdapter<MoneySettingsMode>(mActivity, mList, R.layout.settings_item) {
            @Override
            public void convert(UniversalViewHolder holder, int position, final MoneySettingsMode moneySettingsMode) {
                holder.setText(R.id.tv_settings_name, moneySettingsMode.getCodeName());
                EditText view = holder.getView(R.id.et_settings_money);
                if (moneySettingsMode.getCodeName().equals("发布1条消息费用")
                        || moneySettingsMode.getCodeName().equals("司机临时发货费用")
                        || moneySettingsMode.getCodeName().equals("查车收费标准")
                        || moneySettingsMode.getCodeName().equals("查询司机收费标准")) {
                    double i = Double.parseDouble(moneySettingsMode.getCodeValue());
                    int jf = (int) (i * 100);
                    view.setText(jf + "");
                } else {
                    view.setText(moneySettingsMode.getCodeValue());
                }
                final EditText etSettings = holder.getView(R.id.et_settings_money);
                holder.getView(R.id.tv_settings).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        settings(moneySettingsMode, etSettings);
                    }
                });
            }
        };
        settingsListView.setAdapter(mAdapter);
    }

    /**
     * 设置
     *
     * @param moneySettingsMode
     */
    private void settings(MoneySettingsMode moneySettingsMode, EditText editText) {
        String trim = editText.getText().toString().trim();
        if (TextUtils.isEmpty(trim)) {
            showToast("数据不能为空");
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("Id", moneySettingsMode.getId());

        if (moneySettingsMode.getCodeName().equals("发布1条消息费用")
                || moneySettingsMode.getCodeName().equals("司机临时发货费用")
                || moneySettingsMode.getCodeName().equals("查车收费标准")
                || moneySettingsMode.getCodeName().equals("查询司机收费标准")) {
            double i = Double.parseDouble(trim);
            double jf = i / 100;
            params.put("CodeValue", jf + "");
        } else {
            params.put("CodeValue", trim);
        }


        SoapUtils.Post(mActivity, API.UpdateBaseSet, params, new SoapCallback() {
            @Override
            public void onError(String error) {
                Log.e("ee", "");
            }

            @Override
            public void onSuccess(String data) {
                showToast("更新成功");
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_title_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_title_right:
                commit();
                break;
        }
    }

    /**
     * 提交信息
     */
    private void commit() {


    }
}
